/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.spring.form.renderer;

import java.io.IOException;

import org.skatiger.jrove.ResponseWriter;
import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.renderer.Html;
import org.springframework.web.bind.WebDataBinder;

public class InputCheckboxRenderer extends InputSingleCheckedRenderer {
    protected static final String BOUND_VALUE_PROPERTY_NAME = "boundValue";

    @Override
    public void encodeBegin(ViewContext context, UIComponent component)
            throws IOException {

        // invoke super encodBegin, write input element. only for checkbox and radio
        super.encodeBegin(context, component);
        renderFiledMarker(context, component);
    }

    protected void renderFiledMarker(ViewContext context, UIComponent component)
            throws IOException {
        ResponseWriter writer = context.getResponseWriter();

        boolean disabled = (Boolean) component.getAttributes().get(
                Html.DISABLED_ATTR);
        //write input hidden for singleChecked
        if (!disabled) {
            // Write out the 'field was present' marker.
            writer.startElement(Html.INPUT_ELEM, component);
            //write hidden name
            String name = (String) component.getAttributes()
                    .get(Html.NAME_ATTR);
            writer.writeAttribute(Html.NAME_ATTR,
                    WebDataBinder.DEFAULT_FIELD_MARKER_PREFIX + name,
                    Html.NAME_ATTR);
            //writer hidden
            writer.writeAttribute(Html.TYPE_ATTR, Html.INPUT_TYPE_HIDDEN,
                    Html.TYPE_ATTR);
            //write value

            writer.writeAttribute(Html.VALUE_ATTR, "on", Html.VALUE_ATTR);
            writer.endElement(Html.INPUT_ELEM);
        }
    }

    @Override
    protected void renderValueAttribute(ViewContext context,
            UIComponent component) throws IOException {
        ResponseWriter writer = context.getResponseWriter();

        Object boundValue = component.getAttributes().get(
                BOUND_VALUE_PROPERTY_NAME);
        Class valueType = getBindStatus(component).getValueType();
        if (Boolean.class.equals(valueType) || boolean.class.equals(valueType)) {
            // the concrete type may not be a Boolean - can be String
            if (boundValue instanceof String) {
                boundValue = Boolean.valueOf((String) boundValue);
            }
            Boolean booleanValue = (boundValue != null ? (Boolean) boundValue
                    : Boolean.FALSE);
            writer.writeAttribute(Html.VALUE_ATTR, "true", null);
            if (booleanValue) {
                writer.writeAttribute(Html.CHECKED_ATTR, Boolean.TRUE, null);
            }
        } else {
            renderFromValue(context, component);
        }
    }
}
